const crypto = require("crypto");
const db = uniCloud.database();


module.exports = {
	_before: function() {

	},
	async exec(params,appId,apiKey) {
		let timestamp = new Date().getTime();
		let data = JSON.stringify(params)
		let sign = crypto.createHash('md5').update(appId + data + timestamp + apiKey, 'utf8').digest('hex')
			.toLowerCase();
		let url = 'https://haoshun.vip/API'
		url += "/credit"
		url += '?appid=' + appId;
		url += '&data=' + data;
		url += '&timestamp=' + timestamp;
		url += '&sign=' + sign;
		console.log(url)
		return await uniCloud.httpclient.request(url, {
			headers: {
				"content-type": "application/x-www-form-urlencoded",
			},
			rejectUnauthorized:false,
			method: 'POST',
			data: null,
			contentType: 'json', // 指定以application/json发送data内的数据
			dataType: 'json' // 指定返回值为json格式，自动进行parse
		})
	},
	async submit(params,appId,apiKey) {
		let timestamp = new Date().getTime();
		let data = JSON.stringify(params)
		let sign = crypto.createHash('md5').update(appId + data + timestamp + apiKey, 'utf8').digest('hex')
			.toLowerCase();
		let url = 'https://haoshun.vip/API'
		url += "/submit"
		url += '?appid=' + appId;
		url += '&data=' + data;
		url += '&timestamp=' + timestamp;
		url += '&sign=' + sign;
		console.log(url)
		return await uniCloud.httpclient.request(url, {
			headers: {
				"content-type": "application/x-www-form-urlencoded",
			},
			rejectUnauthorized:false,
			method: 'POST',
			data: null,
			contentType: 'json', // 指定以application/json发送data内的数据
			dataType: 'json' // 指定返回值为json格式，自动进行parse
		})
	},
	async order(params,appId,apiKey) {
		let timestamp = new Date().getTime();
		let data = JSON.stringify(params)
		let sign = crypto.createHash('md5').update(appId + data + timestamp + apiKey, 'utf8').digest('hex')
			.toLowerCase();
		let url = 'https://haoshun.vip/API'
		url += "/order"
		url += '?appid=' + appId;
		url += '&data=' + data;
		url += '&timestamp=' + timestamp;
		url += '&sign=' + sign;
		return await uniCloud.httpclient.request(url, {
			headers: {
				"content-type": "application/x-www-form-urlencoded",
			},
			rejectUnauthorized:false,
			method: 'POST',
			data: null,
			contentType: 'json', // 指定以application/json发送data内的数据
			dataType: 'json' // 指定返回值为json格式，自动进行parse
		})
	}
}